#include "MainHeader.h"
int InitMap(){
	// Fill in the window class structure with parameters 
	// that describe the main window. 
	
	wcMap.cbSize = sizeof(wcMap);		   // size of structure 
	wcMap.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	wcMap.lpfnWndProc = MapProc;	  // points to window procedure 
	wcMap.cbClsExtra = NULL;				// no extra class memory 
	wcMap.cbWndExtra =NULL; 			   // no extra window memory 
	wcMap.hInstance = hGlobal;		   // handle to instance 
	wcMap.hIcon = NULL; 			 // predefined app. icon 
	wcMap.hCursor = NULL;				// predefined arrow 
	// wcMap.hbrBackground = GetStockObject( WHITE_BRUSH);				   // white background brush 
	wcMap.lpszMenuName =  NULL;    // name of menu resource 
	wcMap.lpszClassName = "MapClass";  // name of window class 
	
	
	// Register the window class. 
	
	RegisterClassEx(&wcMap); 
	//Create the Window
	hwndMap = CreateWindowEx(NULL,"MapClass","Map", WS_BORDER|WS_SIZEBOX |WS_TABSTOP | WS_VISIBLE |WS_HSCROLL|WS_VSCROLL,20,30,384,351,0,0,hGlobal,0);
	
	ShowWindow(hwndMap,SW_SHOW);	
	return 0;
}
LRESULT CALLBACK MapProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	int poo;
	int i=0;
	int dw,dh,x,y,nx,ny;
	dw =0;
	dh = 0;
	x = 0;
	y=0;
	nx=0;
	ny=0;
	RECT que;
    int blitareax=0,blitareay=0;
	char cBuf[200];
		int td=0;
	switch (message) 
	{

		 
	
	case WM_PAINT:
		
		hdc = BeginPaint(hWnd, &ps);
		GetWindowRect(hWnd,&que);
        BitBlt(hdc, 0,0,que.right-que.left,que.bottom-que.top,ThisBackBuffer.DC(),nHScroll[sHMap]*16,nVScroll[sVMap]*16,SRCCOPY);
		DrawTileRect(hdc,mpMap);
		DeleteDC(hdc);
		EndPaint(hWnd, &ps);
		
		break;
	case WM_VSCROLL:	// exact same idea, but V scroll instead of H scroll
		UpdateScroll(hWnd,wParam,1,vsbMap,sVMap);
		InvalidateRect(hWnd,0,1);
	    break;
	case WM_HSCROLL:
		UpdateScroll(hWnd,wParam,NULL,hsbMap,sHMap);
		InvalidateRect(hWnd,0,1);
		break;
	case WM_LBUTTONDOWN:
        
		if (wParam & MK_LBUTTON){
			HandleMapLeftButton(message,wParam,lParam);
		}
            return 0;
	
		break;
	case WM_RBUTTONDOWN:
		if(LOWORD(wParam) == MK_RBUTTON){
			HandleRightClick(hWnd,  message,  wParam, lParam);
		}
		break;
	case WM_RBUTTONUP:
		if(ScrollCheck.value()==1){
				   //Checks where the coords are 
			if(MovingScroll[0]==1){
				if(((GetX(lParam)/16)+ nHScroll[sHMap])<(scroll.Scrolls[MovingScroll[1]].xStart) || 
			   ((GetY(lParam)/16)+ nVScroll[sVMap])<(scroll.Scrolls[MovingScroll[1]].yStart)){
				   MessageBox(hWnd,"Box was too small resetting box to old proptions.","Error",MB_OK);
				   MovingScroll[0]=0;
				   MovingScroll[1]=0;
				   return 0;
				}else{
					 scroll.Scrolls[MovingScroll[1]].xEnd=(GetX(lParam)/16)+1+nHScroll[sHMap]; 
					 scroll.Scrolls[MovingScroll[1]].yEnd=(GetY(lParam)/16)+1+nVScroll[sVMap];
					 MovingScroll[0]=0;
					 MovingScroll[1]=0;
					 DrawRoom();
			   }
			}	
		}/*else if((MP[0]==1)&&(MP[1]==1)){//MP[0]=1;//Has been selected
								   //MP[1]=1;//In use 	
			MP[1]=0;
		}else if(mpMap.Width ==1 && mpMap.Height ==1){

			MP[0]=MP[1]=0;
		}*/
		 
		
	
		break;
	case WM_LBUTTONUP:
		if(MovingSprite[0]){
		 	
			switch((u8)Combos[cSpriteSet].GetListIndex()){
			case 0:
				Enemy1.Enemies[MovingSprite[1]].X=(GetX(lParam)/16)+nHScroll[sHMap];
				Enemy1.Enemies[MovingSprite[1]].Y=(GetY(lParam)/16)+nVScroll[sVMap];
			break;
			case 1:
				Enemy2.Enemies[MovingSprite[1]].X=(GetX(lParam)/16)+nHScroll[sHMap];
				Enemy2.Enemies[MovingSprite[1]].Y=(GetY(lParam)/16)+nVScroll[sVMap];
			break;
			case 2:
				Enemy3.Enemies[MovingSprite[1]].X=(GetX(lParam)/16)+nHScroll[sHMap];
				Enemy3.Enemies[MovingSprite[1]].Y=(GetY(lParam)/16)+nVScroll[sVMap];
			break;

			}
				MovingSprite[0]=0;
				MovingSprite[1]=0;
				DrawRoom();
				return 0;
				
			
		}
			if(MovingDoor[0]==1){
		 	
		
			
			
				Doors[DoorBuf.DoorNotes[MovingDoor[1]]].DHeight-=	Doors[DoorBuf.DoorNotes[MovingDoor[1]]].YEntrance;
				Doors[DoorBuf.DoorNotes[MovingDoor[1]]].DHeight+=	(GetY(lParam)/16)+nVScroll[sVMap];

				Doors[DoorBuf.DoorNotes[MovingDoor[1]]].DWidth-=     Doors[DoorBuf.DoorNotes[MovingDoor[1]]].XEntrance; 
				Doors[DoorBuf.DoorNotes[MovingDoor[1]]].DWidth+=	 (GetX(lParam)/16)+nHScroll[sHMap];//-Doors[DoorBuf.DoorNotes[i]].YEntrance)+1;;
				
				Doors[DoorBuf.DoorNotes[MovingDoor[1]]].XEntrance=   (GetX(lParam)/16)+nHScroll[sHMap];
				Doors[DoorBuf.DoorNotes[MovingDoor[1]]].YEntrance=   (GetY(lParam)/16)+nVScroll[sVMap];
				
				MovingDoor[0]=0;
				MovingDoor[1]=0;
				DrawRoom();
				return 0;
			}
			if(ScrollCheck.value()==1){
				if(MovingScroll[0]==1){	
               
				scroll.Scrolls[MovingScroll[1]].xEnd-=scroll.Scrolls[MovingScroll[1]].xStart;
				scroll.Scrolls[MovingScroll[1]].xEnd+=(GetX(lParam)/16)+nHScroll[sHMap];
				scroll.Scrolls[MovingScroll[1]].yEnd-=scroll.Scrolls[MovingScroll[1]].yStart;
				scroll.Scrolls[MovingScroll[1]].yEnd+=(GetY(lParam)/16)+nVScroll[sVMap];
				scroll.Scrolls[MovingScroll[1]].xStart=(GetX(lParam)/16)+nHScroll[sHMap];
				scroll.Scrolls[MovingScroll[1]].yStart=(GetY(lParam)/16)+nVScroll[sVMap];
			
				MovingScroll[0]=0;
				MovingScroll[1]=0;
				DrawRoom();
				}
				return 0;
			}
		break;
	case WM_KEYDOWN:							// Here's what you're interested in! -- check to see if someone pressed a key.
	//c=67
    //ctrl=17
	//p=80
		switch(keyno){
		case 0:
            if(wParam==VK_CONTROL) keyno++;
			break;

		case 1:
			
			if(wParam==67 || wParam==86 || wParam== 0x5a){
				
				EditLayers(wParam,lParam);	

			}
			keyno=0;
			break;
		}
							// set the key that person pressed as true.
			return 0;						
	

		break;
	case WM_MOUSEMOVE:
		if(wParam & (MK_RBUTTON)){
	
			mpMap.nX = GetX(lParam)/16;
			mpMap.nY = GetY(lParam)/16;
			//Determine direction for length and width;
			if(mpMap.nX>mpMap.cX){//increase width;
				mpMap.Width+=1;
			}else if(mpMap.nX<mpMap.cX){
				mpMap.Width-=1;
			}
			if(mpMap.nY>mpMap.cY){//increase width;
				mpMap.Height+=1;
			}else if(mpMap.nY<mpMap.cY){
				mpMap.Height-=1;
			}
			mpMap.cX = GetX(lParam)/16;
			mpMap.cY = GetY(lParam)/16;
			if(mpMap.Height<1) mpMap.Height =1;
			if(mpMap.Width<1)mpMap.Width = 1;
		//	if(mpMap.Width>16)mpMap.Width = 16;
		//	if(mpMap.Height>16)mpMap.Height = 16;
			//check for out of bounds 
			if(mpMap.nX <0) mpMap.nX = 0;
			if(mpMap.nX>ThisLevelData->X) 
				mpMap.nX = ThisLevelData->X;
			if(mpMap.nY <0) mpMap.nY = 0;
			if(mpMap.nY>ThisLevelData->Y) mpMap.nY = ThisLevelData->Y;
		}else{
			mpMap.sY = GetY(lParam)/16;
			mpMap.sX = GetX(lParam)/16;
		}
		poo =	Combos[cClip].GetListIndex();
		Combos[cClip].SetListIndex(ThisClipData->TileBuf2D[((GetX(lParam)/16)+ nHScroll[sHMap]) +  ((GetY(lParam)/16)+ nVScroll[sVMap])* (ThisLevelData->X) ]);
		
		GetWindowText(Combos[cClip].thishwnd(),cBuf,200);
		SetWindowText(GetDlgItem(Main,lblClip),cBuf);
		Combos[cClip].SetListIndex(poo);
		sprintf(cBuf,"X: %X",((GetX(lParam)/16)+ nHScroll[sHMap]));
		SetWindowText(GetDlgItem(Main,lblX),cBuf);
		sprintf(cBuf,"Y: %X",((GetY(lParam)/16)+ nVScroll[sVMap]));
		SetWindowText(GetDlgItem(Main,lblY),cBuf);
     	if(wParam == MK_LBUTTON) SendMessage(hWnd, 0x201, wParam, lParam);
	
		InvalidateRect(hwndMap,0,0);
		
		break;
	case WM_SIZE:
		//Gotta Make sure the size isn't out of bounds.
		if(lParam!=0){
			GetWindowRect(hWnd,&que);
			que.right = que.right - que.left;
			que.bottom = que.bottom - que.top;
			if((que.right/16)+nVScroll[1]>ThisLevelData->X) {
			que.right  -= ((que.right/16) +nHScroll[1]- ThisLevelData->X   - 1)*16;
			que.right >>=4;
			que.right <<=4;
			}
			if((que.bottom/16)+nVScroll[2]>ThisLevelData->Y){
			que.bottom  -= ((que.bottom/16)+nVScroll[2]- ThisLevelData->Y   - 1)*16;
			que.bottom >>=4;
			que.bottom <<=4;
			}
			//if((que.bottom/16)>ThisLevelData->Y) que.bottom = (ThisLevelData->Y / 16)*16 +16;//+20;
			MoveWindow(hWnd,que.left,que.top,que.right,que.bottom,1);//+16 is for scrollbar
			/*Take the current size 
			  Vertical (que.bottom/16
			  We are displaying (que.bottom/16) tiles 
			  There's a total of ThisLevelData->Y tiles
			*/
			//GetWindowRect(hWnd,&que);
			//que.right = que.right - que.left;
			//que.bottom = que.bottom - que.top;
			nMaxHScroll[sHMap] = ThisLevelData->X-( (que.right/16)); // maximum H scroll
			
			
			nMaxVScroll[sVMap] = ThisLevelData->Y-((que.bottom/16));
			
			
			UpdateScroll(hWnd,wParam,1,vsbMap,sVMap);
		
			UpdateScroll(hWnd,wParam,NULL,hsbMap,sHMap);
		}
		
		
		break;
	case WM_DESTROY:
		
		break;
		

		
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}
